<?php
/**
 * DBShop 电子商务系统
 *
 * ==========================================================================
 * @link      https://www.dbshop.net/
 * @copyright 北京珑大钜商科技有限公司，并保留所有权利。
 * @license   https://www.dbshop.net/license.html License
 * ==========================================================================
 *
 * @author    静静的风 <baron@loongdom.cn>
 *
 */

namespace Admin\Event;

use Laminas\EventManager\Event;
use Laminas\EventManager\EventManagerInterface;
use Laminas\EventManager\ListenerAggregateInterface;

class AdminListener implements ListenerAggregateInterface
{
    protected $listeners = [];

    public function __construct()
    {
    }

    public function attach(EventManagerInterface $events, $priority = 1)
    {
        $shareEvents = $events->getSharedManager();

        $this->listeners[] = $shareEvents->attach(
            'Admin\Controller\PaymentController', 'admin-payment.edit.show', [$this, 'onExtendPayment'], 99
        );

        $this->listeners[] = $shareEvents->attach(
            'Admin\Controller\PaymentController', 'admin-payment.edit.post', [$this, 'onExtendPaymentSave'], 99
        );
    }

    public function detach(EventManagerInterface $events)
    {
        foreach ($this->listeners as $index => $listener) {
            $events->detach($listener);
            unset($this->listeners[$index]);
        }
    }

    public function onExtendPayment(Event $event)
    {
        $array      = $event->getParams();
        $configArray= $array['configArray'];
        $paymentCode= ucfirst(basename($array['paymentCode']));

        $paymentConfigState = false;
        switch ($paymentCode) {
            case 'Wxpay'://微信支付扩展
                if (!isset($configArray['paymentPublicKey'])) {
                    $paymentConfigState = true;
                    $configArray['originalRefundState'] = [
                        'title' => '售后原路退款',
                        'content' => [
                            'radio1' => [
                                'name' => '启用',
                                'value' => '1',
                            ],
                            'radio2' => [
                                'name' => '禁用',
                                'value' => '0',
                            ],
                        ],
                        'nameId' => 'originalRefundState',
                        'inputType' => 'radio',
                        'checked' => '0',
                        'width' => 'col-sm-2',
                        'info'  => '用于售后申请退款处理。'
                    ];
                    $configArray['cancelOrderState'] = [
                        'title' => '订单取消原路退款',
                        'content' => [
                            'radio1' => [
                                'name' => '启用',
                                'value' => '1',
                            ],
                            'radio2' => [
                                'name' => '禁用',
                                'value' => '0',
                            ],
                        ],
                        'nameId'    => 'cancelOrderState',
                        'inputType' => 'radio',
                        'checked'   => '0',
                        'width' => 'col-sm-2',
                        'info'      => '已经付款未发货的实物商品订单。'
                    ];
                    $configArray['paymentPublicKey'] = [
                        'title' => '微信支付公钥',
                        'content' => '',
                        'nameId' => 'paymentPublicKey',
                        'inputType' => 'textarea',
                        'rows'  => 5,
                        'class' => 'form-control',
                        'width' => 'col-sm-6',
                        'info'  => '用于微信退款、微信红包等，如启用，请填写公钥信息。'
                    ];
                    $configArray['paymentPrivateKey'] = [
                        'title' => '微信支付私钥',
                        'content' => '',
                        'nameId' => 'paymentPrivateKey',
                        'inputType' => 'textarea',
                        'rows'  => 5,
                        'class' => 'form-control',
                        'width' => 'col-sm-6',
                        'info'  => '用于微信退款、微信红包等，如启用，请填写私钥信息。'
                    ];
                }
                break;

            case 'Alipay'://支付宝扩展
                if (!isset($configArray['originalRefundState'])) {
                    $paymentConfigState = true;
                    $configArray['originalRefundState'] = [
                        'title' => '售后原路退款',
                        'content' => [
                            'radio1' => [
                                'name' => '启用',
                                'value' => '1',
                            ],
                            'radio2' => [
                                'name' => '禁用',
                                'value' => '0',
                            ],
                        ],
                        'nameId' => 'originalRefundState',
                        'inputType' => 'radio',
                        'checked' => '0',
                        'width' => 'col-sm-2',
                        'info'  => '用于售后申请退款处理。'
                    ];
                    $configArray['cancelOrderState'] = [
                        'title' => '订单取消原路退款',
                        'content' => [
                            'radio1' => [
                                'name' => '启用',
                                'value' => '1',
                            ],
                            'radio2' => [
                                'name' => '禁用',
                                'value' => '0',
                            ],
                        ],
                        'nameId' => 'cancelOrderState',
                        'inputType' => 'radio',
                        'checked' => '0',
                        'width' => 'col-sm-2',
                        'info'      => '已经付款未发货的实物商品订单。'
                    ];
                }
                break;
            case 'BalancePay'://余额支付扩展
                if (!isset($configArray['cancelOrderState'])) {
                    $paymentConfigState = true;
                    $configArray['cancelOrderState'] = [
                        'title' => '订单取消原路退款',
                        'content' => [
                            'radio1' => [
                                'name' => '启用',
                                'value' => '1',
                            ],
                            'radio2' => [
                                'name' => '禁用',
                                'value' => '0',
                            ],
                        ],
                        'nameId' => 'cancelOrderState',
                        'inputType' => 'radio',
                        'checked' => '0',
                        'width' => 'col-sm-2',
                        'info'      => '已经付款未发货的实物商品订单。'
                    ];
                }
                break;
        }

        if ($paymentConfigState) $array['configArray'] = $configArray;
        $event->setParams($array);

        return ['configArray' => $configArray, 'paymentConfigState' => $paymentConfigState];
    }

    /**
     * 支付保存
     * @param Event $event
     * @return void
     */
    public function onExtendPaymentSave(Event $event)
    {
        $paymentArray   = $event->getParam('paymentArray');
        $paymentCode    = ucfirst(basename($event->getParam('paymentCode')));

        if ($paymentCode == 'Wxpay') {
            file_put_contents(getcwd() . '/data/moduleData/Payment/cert/wechat_cert.pem', $paymentArray['paymentPublicKey']);
            file_put_contents(getcwd() . '/data/moduleData/Payment/cert/wechat_key.pem', $paymentArray['paymentPrivateKey']);
        }
    }
}